<template>
    <div class="login-container">
        <div class="login-box">
            <div class="login-title">
                <h2>拍卖交易系统</h2>
            </div>

            <el-form ref="loginFormRef" :model="loginForm" :rules="loginRules" class="login-form">
                <el-form-item prop="username">
                    <el-input v-model="loginForm.username" placeholder="请输入用户名" prefix-icon="User" />
                </el-form-item>

                <el-form-item prop="password">
                    <el-input v-model="loginForm.password" type="password" placeholder="请输入密码" prefix-icon="Lock"
                        show-password />
                </el-form-item>

                <el-form-item prop="userType">
                    <el-radio-group v-model="loginForm.userType">
                        <el-radio label="user">用户登录</el-radio>
                        <el-radio label="admin">管理员登录</el-radio>
                    </el-radio-group>
                </el-form-item>

                <el-form-item>
                    <el-button type="primary" :loading="loading" class="login-button"
                        @click="handleLogin">登录</el-button>
                </el-form-item>

                <div class="login-options">
                    <router-link v-if="loginForm.userType === 'user'" to="/register">没有账号？立即注册</router-link>
                    <div v-else></div>
                </div>
            </el-form>
        </div>
    </div>
</template>

<script setup>
import { ref, reactive } from 'vue'
import { useRouter } from 'vue-router'
import { ElMessage } from 'element-plus'
import { useUserStore } from '@/stores/user'
import { useAdminStore } from '@/stores/admin'

const router = useRouter()
const userStore = useUserStore()
const adminStore = useAdminStore()

const loginFormRef = ref(null)
const loading = ref(false)

const loginForm = reactive({
    username: '',
    password: '',
    userType: 'user' // 默认用户登录
})

const loginRules = {
    username: [
        { required: true, message: '请输入用户名', trigger: 'blur' },
        { min: 3, max: 20, message: '用户名长度应为3-20个字符', trigger: 'blur' }
    ],
    password: [
        { required: true, message: '请输入密码', trigger: 'blur' },
        { min: 6, max: 20, message: '密码长度应为6-20个字符', trigger: 'blur' }
    ],
    userType: [
        { required: true, message: '请选择登录类型', trigger: 'change' }
    ]
}

const handleLogin = async () => {
    if (!loginFormRef.value) return

    await loginFormRef.value.validate(async (valid) => {
        if (!valid) {
            return false
        }

        try {
            loading.value = true

            const loginData = {
                username: loginForm.username,
                password: loginForm.password
            }

            if (loginForm.userType === 'user') {
                // 用户登录
                await userStore.login(loginData)
                router.push('/market')
                ElMessage.success('登录成功')
            } else {
                // 管理员登录
                await adminStore.login(loginData)
                router.push('/admin')
                ElMessage.success('管理员登录成功')
            }
        } catch (error) {
            ElMessage.error(error.message || '登录失败，请检查账号密码')
        } finally {
            loading.value = false
        }
    })
}
</script>

<style scoped>
.login-container {
    display: flex;
    justify-content: center;
    align-items: center;
    height: 100vh;
    background-color: #f5f7fa;
}

.login-box {
    width: 400px;
    padding: 30px;
    background-color: #fff;
    border-radius: 8px;
    box-shadow: 0 2px 12px 0 rgba(0, 0, 0, 0.1);
}

.login-title {
    text-align: center;
    margin-bottom: 30px;
}

.login-title h2 {
    color: #409EFF;
    font-weight: 600;
}

.login-form {
    margin-bottom: 20px;
}

.login-button {
    width: 100%;
    margin-top: 10px;
}

.login-options {
    display: flex;
    justify-content: space-between;
    margin-top: 15px;
    font-size: 14px;
}

.login-options a {
    color: #409EFF;
    text-decoration: none;
}
</style>